<?
// Show the comments entry form if the user is a manager.
function showManagerCommentView() {
	echo "<div class='featured-post'>
            <div class='post-title'>
                <h2>List Events</h2>
			</div>";
	
	// Get the page number so we know which page to display.
	$pageNum = $_GET['pagenum'];
	if (!(isset($pageNum))) { 
		$pageNum = 1; 
	}
		
	$myID = $_SESSION['ls_userid'];
	// Find out this users total number of events that has a comment from the coach.
	$query = "SELECT `eventID` FROM `ls_events` ORDER BY `eventDate` DESC";				
	$list = mysql_query($query) or die("sql error at coach view query 1: " . mysql_error());
	
	// Get the total number of comments
	$totalRows = mysql_num_rows($list);
	
	// Show comments only if there are comments for the current user.
	if($totalRows == 0) {
		echo "<p>There are no comments from your coaches.</p></div>";
	}
	else {
		// Number of results to show per page.
		$rowLimit = 20;
		
		// Number for last page.
		$last = ceil($totalRows / $rowLimit);
		
		// Make sure page number is in range.
		if($pageNum < 1) {
			$pageNum = 1;
		}
		else if ($pageNum > $last) {
			$pageNum = $last;
		}
		
		// Set the limit range for the sql query.
		if($pageNum > 0) {
			$max = 'LIMIT ' .($pageNum - 1) * $rowLimit . ',' . $rowLimit;
		}
		
		// Run query again with the limit to get the correct rows for the current page.
		// The results will be ordered by the comment posting date starting from the most recent.
		$query = "SELECT * FROM `ls_events`
					ORDER BY `ls_events`.`eventDate` DESC $max";
									
		$list = mysql_query($query) or die("Could not select at showCommentList (2nd). " . mysql_error());
		
		// Print out the list of events in an html table.
		echo "<div class='post-entry'>
			<table id='blueTable'><tr><th>Event Name</th><th>Age Group</th><th>Event Type</th><th>Length</th><th>Date</th><th>Swimmers</th></tr>";
		$i = 0;
		while($rows = mysql_fetch_assoc($list)) {
			// Use the $style variable to alternate the css class.
			$style = (($i % 2) == 0)? 'even' : 'odd';
			echo "<tr class='$style'><td>" . $rows['name'] . "</td><td>" . $rows['ageGroup'] . "</td><td>" . $rows['eventType'] . "</td><td>" . $rows["eventLength"] . 
				"</td><td>" . $rows['eventDate'] . "</td><td><a href='" . substr($_SERVER['PHP_SELF'], 0, lastIndexOf($_SERVER['PHP_SELF'], '/')) . "/enterComments.php?eventID=" . $rows['eventID'] . "'>View Swimmers</a></td></tr>";
			$i++;
		}
		echo "</table></div>";
		
		// Do pagination if there are more than 1 pages of results to show.
		if($last > 1) {
			paginate($pageNum, $last);
		}
	}
	echo "</div>";
}

// Get the last index of a character in a string.
function lastIndexOf($string,$item){  
    $index=strpos(strrev($string),strrev($item));  
    if ($index){  
        $index=strlen($string)-strlen($item)-$index;  
        return $index;  
    }  
    else { 
        return -1;
	}		
}
?>